package com.wu.wechat.service.impl;

import com.wu.wechat.common.Constant;
import com.wu.wechat.pojo.response.Article;
import com.wu.wechat.pojo.response.NewsMessage;
import com.wu.wechat.pojo.response.TextMessage;
import com.wu.wechat.service.CoreService;
import com.wu.wechat.util.MessageUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Create by wuyanshen
 * @Date 2018-08-07 上午10:59
 * @Description <描述>
 */
@Service("coreService")
public class CoreServiceImpl implements CoreService {

    public static Logger log = Logger.getLogger(CoreServiceImpl.class);


    @Override
    public String processRequest(HttpServletRequest request) {
        String respMessage = null;
        try {
            // xml请求解析
            Map<String, String> requestMap = MessageUtil.parseXml(request);

            // 发送方帐号（open_id）
            String fromUserName = requestMap.get("FromUserName");
            // 公众帐号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");

            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            textMessage.setCreateTime(new Date().getTime());
            textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);
            textMessage.setFuncFlag(0);
            // 文本消息
            if (msgType.equals(Constant.REQ_MESSAGE_TYPE_TEXT)) {
                // 接收用户发送的文本消息内容
                String content = requestMap.get("Content");

                if ("1".equals(content)) {
                    textMessage.setContent("1是很好的");
                    // 将文本消息对象转换成xml字符串
                    respMessage = MessageUtil.textMessageToXml(textMessage);
                }else if ("2".equals(content)) {
                    textMessage.setContent("我不是2货");
                    // 将文本消息对象转换成xml字符串
                    respMessage = MessageUtil.textMessageToXml(textMessage);
                }

                // 创建图文消息
                NewsMessage newsMessage = new NewsMessage();
                newsMessage.setToUserName(fromUserName);
                newsMessage.setFromUserName(toUserName);
                newsMessage.setCreateTime(new Date().getTime());
                newsMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_NEWS);
                newsMessage.setFuncFlag(0);

                List<Article> articleList = new ArrayList<Article>();
                // 单图文消息
                if ("文章1".equals(content)) {
                    Article article = new Article();
                    article.setTitle("我是一条单图文消息");
                    article.setDescription("我是描述信息，哈哈哈哈哈哈哈。。。");
                    article.setPicUrl("http://www.iteye.com/upload/logo/user/603624/2dc5ec35-073c-35e7-9b88-274d6b39d560.jpg");
                    article.setUrl("http://tuposky.iteye.com");
                    articleList.add(article);
                    // 设置图文消息个数
                    newsMessage.setArticleCount(articleList.size());
                    // 设置图文消息包含的图文集合
                    newsMessage.setArticles(articleList);
                    // 将图文消息对象转换成xml字符串
                    respMessage = MessageUtil.newsMessageToXml(newsMessage);
                }
                // 多图文消息
                else if ("文章2".equals(content)) {
                    Article article1 = new Article();
                    article1.setTitle("我是一条多图文消息");
                    article1.setDescription("");
                    article1.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20130913/2013091314543416032.jpg");
                    article1.setUrl("http://tuposky.iteye.com/blog/2008583");

                    Article article2 = new Article();
                    article2.setTitle("微信公众平台开发教程Java版（二）接口配置 ");
                    article2.setDescription("");
                    article2.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20131021/2013102111243367254.jpg");
                    article2.setUrl("http://tuposky.iteye.com/blog/2008655");

                    Article article3 = new Article();
                    article3.setTitle("微信公众平台开发教程Java版(三) 消息接收和发送");
                    article3.setDescription("");
                    article3.setPicUrl("http://www.isic.cn/viewResourcesAction//logo/20131021/2013102111291287031.jpg");
                    article3.setUrl("http://tuposky.iteye.com/blog/2017429");

                    articleList.add(article1);
                    articleList.add(article2);
                    articleList.add(article3);
                    newsMessage.setArticleCount(articleList.size());
                    newsMessage.setArticles(articleList);
                    respMessage = MessageUtil.newsMessageToXml(newsMessage);
                }else{
                    textMessage.setContent("你好啊，兄弟");
                    // 将文本消息对象转换成xml字符串
                    respMessage = MessageUtil.textMessageToXml(textMessage);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return respMessage;
    }

}
